package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsIndustryInfoMapper;
import cc.rengu.igas.bsps.common.entity.IndustryInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 行业类别信息表操作接口实现类
 * Created by 王成 on 2020/3/10
 */
public class BspsIndustryInfoMapperImpl implements BspsIndustryInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertBspsIndustryInfo(IndustryInfo industryInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBspsIndustryInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, industryInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记行业类别信息失败,industryType:<{}>,industryDesc:<{}>,id:<{}>,iReturnCode:<{}>",
                    industryInfo.getIndustryType(), industryInfo.getIndustryDesc(), industryInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateBspsIndustryInfoById(IndustryInfo industryInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspsIndustryInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, industryInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新行业类别信息失败,industryType:<{}>,industryDesc:<{}>,id:<{}>,iReturnCode:<{}>",
                    industryInfo.getIndustryType(), industryInfo.getIndustryDesc(), industryInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public IndustryInfo selectBspsIndustryInfoById(String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsIndustryInfoById");
        IndustryInfo industryInfo = new IndustryInfo();
        industryInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, industryInfo, industryInfo);
        if (0 != iReturnCode) {
            rglog.error("获取行业类别信息失败,id:<{}>,iReturnCode:<{}>", id, iReturnCode);
            return null;
        }
        return industryInfo;
    }

    @Override
    public IndustryInfo selectBspsIndustryInfoByIndustryType(String industryType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsIndustryInfoByIndustryType");
        IndustryInfo industryInfo = new IndustryInfo();
        industryInfo.setIndustryType(industryType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, industryInfo, industryInfo);
        if (0 != iReturnCode) {
            rglog.error("获取行业类别信息失败,industryType:<{}>,iReturnCode:<{}>", industryType, iReturnCode);
            return null;
        }
        return industryInfo;
    }
}
